package com.bifrost.repository;

import com.bifrost.constant.BonusPointsUpdatedType;
import com.bifrost.entity.BonusPointsHistory;
import org.hibernate.sql.Update;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author rangf
 * @date 2018/1/9
 */
public interface BonusPointsHistoryRepository extends JpaRepository<BonusPointsHistory, Long>, JpaSpecificationExecutor<BonusPointsHistory> {


    /**
     * 获取指定会员卡在门店所有积分记录
     * @param merchantId
     * @param cardNumber
     * @return
     */
    List<BonusPointsHistory> findAllByMerchantIdAndCardNumber(Long merchantId, String cardNumber);

    /**
     * 查询记录
     * @param number
     * @param cardNumber
     * @param type
     * @return
     */
    BonusPointsHistory findFirstByNumberAndCardNumberAndUpdatedType(String number, String cardNumber, BonusPointsUpdatedType type);

    /**
     * 获取积分历史记录
     * @param number 积分原单号
     * @param type 积分类型
     * @return
     */
    BonusPointsHistory findFirstByNumberAndUpdatedType(String number, BonusPointsUpdatedType type);

    @Modifying
    @Transactional
    @Query(value = "update bonus_points_history set CREATE_BY=?1 WHERE BONUSPOINTS_HISTORY_ID=?2",nativeQuery = true)
    int updateOptNameById(String createBy,Long id);
}
